
using System;
using System.Linq;
using Csla;
using Csla.Data;
using HousingInfo.DataLinQ;

namespace HousingInfo.Library
{ 
	public partial class ProjectBOList : Csla.BusinessListBase<ProjectBOList, ProjectBO>
	{

		#region Authorization Rules

		protected static void AddObjectAuthorizationRules()
		{
			//TODO: Define object-level authorization rules of ProjectBOList
			//AuthorizationRules.AllowCreate(typeof(ProjectBOList), "ProjectBOListCreateGroup"));
			//AuthorizationRules.AllowEdit(typeof(ProjectBOList), "ProjectBOListEditGroup"));
			//AuthorizationRules.AllowDelete(typeof(ProjectBOList), "ProjectBOListDeleteGroup"));
		}
		#endregion //Authorization Rules

        #region Ham lay tat ca tin va tin chua duyet - aDMIN
        public static ProjectBOList Tatca_chuaduyet(bool tatca)
        {
            return DataPortal.Fetch<ProjectBOList>(new FilterCriteria_Tatca_chuaduyet(tatca));
        }
        #region khai bao bien
        private class FilterCriteria_Tatca_chuaduyet
        {
            public bool tatca;
            public FilterCriteria_Tatca_chuaduyet(bool tc)
            {
                this.tatca = tc;
            }
        }
        #endregion
        #region Truy Cap Du Lieu
        private void DataPortal_Fetch(FilterCriteria_Tatca_chuaduyet cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                if (cretia.tatca == true)
                {
                    this.AddRange(
                        from row in mgr.DataContext.Projects
                        orderby row.CreatedDate descending
                        select ProjectBO.GetProjectBO(row.ProjectID)
                    );
                }
                else
                {
                    this.AddRange(
                        from row in mgr.DataContext.Projects
                         where row.IsActive == false
                         orderby row.CreatedDate descending
                         select ProjectBO.GetProjectBO(row.ProjectID)
                    );
                }
                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion

        #region Ham hien thi danh sach du an theo: tat ca - danh muc - co phan trang
        #region khai bao ham
        public static ProjectBOList DanhSachDuan_PhanTrang(int idcate, int trang, int soluong)
        {
            return DataPortal.Fetch<ProjectBOList>(new FilterCriteria_DanhsachDuAn(idcate, trang, soluong));
        }
        #endregion

        #region khai bao bien
        private class FilterCriteria_DanhsachDuAn
        {
            public int idcategory;
            public int sotrang;
            public int soluongtin;
            public FilterCriteria_DanhsachDuAn(int id, int st, int sl)
            {
                this.idcategory = id;
                this.sotrang = st;
                this.soluongtin = sl;
            }
        }
        #endregion

        #region truy cap du lieu
        private void DataPortal_Fetch(FilterCriteria_DanhsachDuAn cretia)
        {
            bool cancel = false;
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                var result = from row in mgr.DataContext.Projects
                             where row.IsActive == true
                             select row;
                result = from row in result
                         orderby row.CreatedDate descending
                         select row;
                if (cretia.idcategory != -1)
                {
                    result = from row in result                             
                             where row.CategoryID == cretia.idcategory
                             select row;
                }

                if (cretia.soluongtin == -1 && cretia.sotrang == -1)
                {
                    this.AddRange(
                        from row in result
                        select ProjectBO.GetProjectBO(row.ProjectID)
                    );
                }
                else
                {
                    this.AddRange(
                        (from row in result
                        select ProjectBO.GetProjectBO(row.ProjectID)).Skip((cretia.sotrang - 1)*cretia.soluongtin).Take(cretia.soluongtin)
                    );
                }
                RaiseListChangedEvents = true;
            }//using
        }
        #endregion
        #endregion


        #region Dem so tin theo danh muc
        public static int DemSoDuAnTheoDanhMuc(int cateid)
        {
            int count = 0;
            using (var mgr = ContextManager<HousingInfoDataContext>
                    .GetManager(Database.HousingInfoConnection))
            {
                count = mgr.DataContext.Projects.Count(n => n.CategoryID == cateid);
            }

            return count;
        }
        #endregion 
	}
}
